<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="net.sf.rose.business.service.DBMetaService" %>
<%@ include file="/common/header.jsp"%>
<c:set var="path" value="..${ctx}/define/database"/>

<script type="text/javascript">

</script>

<div class="pageContent">

	<div class="panelBar">
		<ul class="toolBar">
			<li><a class="edit" onclick="saveDatabaseTable();" title="保存"><span>保存</span></a></li>
			<li class="line">line</li>
			<li><a class="delete" onclick="deleteDatabaseTable();" title="保存"><span>删除</span></a></li>
		</ul>
	</div>

	<form name="action_set_form" method="post" action="${path}/save.html" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone);">
		<input type="hidden" id="actionType" name="actionType" value="${actionType}" />
		<div class="pageFormContent" layoutH="300" >
			<fieldset>
				<dl >
					<dt>表名称：</dt>
					<dd><input name="name" id="name" type="text" size="60" value="${bean.name}" class="required" /></dd>
				</dl>
				<dl class="nowrap">
					<dt>表说明：</dt>
					<dd><input name="text" type="text" size="60" value="${bean.text}" class="required" /></dd>
				</dl>
				<dl class="nowrap">
					<dt>主键策略：</dt>
					<dd><input name="keyGenerate" type="text" size="60" value="${bean.keyGenerate}" /> 
					<font color="blue" size="5">{yyyyMMdd}+[0001]+(****)+EN</font>
					</dd>
				</dl>
				<dl class="nowrap">
					<dt>描述：</dt>
					<dd><textarea name="description" cols="80" rows="3">${bean.description}</textarea></dd>
				</dl>
			</fieldset>
		</div>

		<div class="pageFormContent" layoutH="200">
		<div class="tabs">
			<div class="tabsHeader">
				<div class="tabsHeaderContent">
					<ul>
						<li class="selected"><a href="javascript:void(0)"><span>字段列表</span></a></li>
					</ul>
				</div>
			</div>
			<div class="tabsContent" style="height:200px;">
				<div>
					<table class="list nowrap itemDetail" addButton="添加参数" width="100%">
						<thead>
							<tr>
								<th type="text" name="items.name[#index#]" size="20" fieldClass="required">字段名称</th>
								<th type="text" name="items.text[#index#]" size="20" fieldClass="required">字段说明</th>
								<th type="enum" name="items.type[#index#]" enumUrl="..${ctx}/common/view.html?jsp=define/dbmeta/data_type_list" size="14" >数据类型</th>
								<th type="text" name="items.size[#index#]" size="4">长度</th>
								<th type="enum" name="items.pk[#index#]" enumUrl="..${ctx}/common/view.html?jsp=base/com/yes_no_list" size="2" >主键</th>
								<th type="enum" name="items.canNull[#index#]" enumUrl="..${ctx}/common/view.html?jsp=base/com/yes_no_list" size="2" >空值</th>
								<th type="text" name="items.defaultValue[#index#]" size="8">默认值</th>
								<th type="lookup" name="items.linkField[#index#].name" lookupGroup="items.linkField[#index#]" lookupPk="" lookupUrl="..${ctx}/common/view.html?jsp=define/database/table_field_tree" size="20">连接字段</th>
								<th type="enum" name="items.linkType[#index#]" enumUrl="..${ctx}/common/view.html?jsp=define/dbmeta/table_link_list" size="8" >连接类型</th>
								<th type="del" width="30">操作</th>
							</tr>
						</thead>
						<tbody>
							<c:if test="${! empty bean.fields }">
							<c:forEach var="data" items="${bean.fields}" varStatus="status">
							<tr>
								<td>
									<input type="text" class="required textInput" size="20" value="${data.name}" name="items.name[${status.index}]" />
								</td>
								<td><input type="text" class="required textInput" size="20" value="${data.text}" name="items.text[${status.index}]" /></td>
								<td>
									<select name="items.type[${status.index}]" class="combox">
										<%request.setAttribute("dataTypes", DBMetaService.getService().getAllDataType());%>
										<c:forEach var="type" items="${dataTypes}">
											<option value="${type}" <c:if test="${type==data.type}">selected</c:if> >${type}</option>
										</c:forEach>
									</select>
								</td>
								<td><input type="text" class="textInput" size="4" value="${data.size}" name="items.size[${status.index}]" /></td>
								<td>
									<select name="items.pk[${status.index}]" class="combox">
										<option value="1" <c:if test="${data.pk}">selected</c:if> >是</option>
										<option value="0" <c:if test="${!data.pk}">selected</c:if> >否</option>
									</select>
								</td>
								<td>
									<select name="items.canNull[${status.index}]" class="combox">
										<option value="1" <c:if test="${data.canNull}">selected</c:if> >是</option>
										<option value="0" <c:if test="${!data.canNull}">selected</c:if> >否</option>
									</select>
								</td>
								<td><input type="text" class="textInput" size="8" value="${data.defaultValue}" name="items.defaultValue[${status.index}]" /></td>
								<c:set var="linkField_name" value="" />
								<c:set var="linkField_text" value="" />
								<c:set var="linkType" value="1" />
								<c:if test="${! empty data.link }">
									<c:set var="linkField_name" value="${data.link.targetTable.name}.${data.link.targetField.name}" />
									<c:set var="linkField_text" value="${data.link.targetTable.text}.${data.link.targetField.text}" />
									<c:set var="linkType" value="${data.link.type}" />
								</c:if>
								<td>
									<input type="hidden" value="${linkField_name}" name="items.linkField[${status.index}].id" />
									<input type="text" class="textInput" size="20" value="${linkField_text}" name="items.linkField[${status.index}].name" />
									<a class="btnLook" href="..${ctx}/common/view.html?jsp=define/database/table_field_tree" lookupGroup="items.linkField[${status.index}]">数据库表结构</a>
								</td>
								<td>
									<select name="items.linkType[${status.index}]" class="combox">
										<option value="1" <c:if test="${linkType==1}">selected</c:if> >一对一</option>
										<option value="2" <c:if test="${linkType==2}">selected</c:if> >一对多</option>
										<option value="3" <c:if test="${linkType==3}">selected</c:if> >多对一</option>
									</select>
								</td>
								<td><a class="btnDel " href="javascript:void(0)">删除</a></td>
							</tr>
							</c:forEach>
							</c:if>
						</tbody>
					</table>
				</div>

			</div>

			<div class="tabsFooter">
				<div class="tabsFooterContent"></div>
			</div>
		</div>

		</div>

	</form>
</div>
<script type="text/javascript">
/**
 * dialog上的表单提交回调函数
 * 服务器转回navTabId，可以重新载入指定的navTab. statusCode=DWZ.statusCode.ok表示操作成功, 自动关闭当前dialog
 * form提交后返回json数据结构,json格式和navTabAjaxDone一致
 */
function dialogAjaxDone(json){
	DWZ.ajaxDone(json);
	if (json.statusCode == DWZ.statusCode.ok){
		// alertMsg.correct(json.message);
		if(json.callbackType){
			var str = $("#name").val();
			var url = "${path}/search.html?name=" + str;
			navTab.reload(url,null,"base_define_view_list");
			// navTab.reload(url);
			//navTab.reloadFlag("base_define_action_list"); // 下次切换到列表页签时需要刷新一下
			setTimeout(function(){$.pdialog.closeCurrent();}, 100); // 关闭此编辑页签
		}
	}
}

function saveDatabaseTable(){
	$(action_set_form).submit();
}

function deleteDatabaseTable(){
	var rel = '';
	$('.base_define_view_list_tr').each(function(){
		if($(this).hasClass("selected")){
			rel = $(this).attr('rel');
		}
	});
	if(rel==''){
		alertMsg.warn('请先选择一条数据！');
	}else{
		alertMsg.confirm("确定要删除吗?", {
			okCall: function(){
				var url = "${path}/delete.html?name=" + rel;
				$.ajax({
					type: "post", url: url,
					success: function(json){
						alertMsg.correct(json.msg);
						var url = "${path}/search.html";
						navTab.reload(url);
						setTimeout(function(){$.pdialog.closeCurrent();}, 100); // 关闭此编辑页签
					},
					error: function(){
						alertMsg.error('系统错误，请联系管理员！');
					}
				});
			}
		});
	}
}

</script>
